package org.jeecg.modules.simulator.model.line.station.strategy;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.simulator.model.MetroTrain;
import org.jeecg.modules.simulator.model.OffTrainNumRange;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.person.Person;
import org.jeecg.modules.simulator.model.person.path.PersonState;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @program: gp2
 * @description:
 * @author: renzhg
 * @create: 2024-09-10 13:17
 **/
@Slf4j
public class ClearOffTrainStrategy implements ITrainStrategy{
    private static final Logger logger = LoggerFactory.getLogger(ClearOffTrainStrategy.class);
    @Override
    public boolean trainExchange(MetroTrain metroTrain,
                                 Map<String, OffTrainNumRange> offTrainNumRangeMap ,
                                 Map<String, Station> stationNameMap ,
                                 Station station, LocalTime now) {

        int personNum=  getOffTrainNum(now.toString(),offTrainNumRangeMap,0) ;
        boolean retboo = true;
        try {
            List<String> keyDelList = new ArrayList<>();
            List<Person> offTrainList = new ArrayList<>();
            getOffTrainClearPassengers(station.getName(), personNum,metroTrain).forEach(o -> {
                offTrainList.add(o);
                keyDelList.add(o.getId());
            });
            exitStation(offTrainList, station);
            keyDelList.forEach(o -> {
                metroTrain.removePassengerFlow(o);
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

        return retboo;
//        return false;
    }


    //从下车人数范围中获取每秒下车规定的范围
    private int getOffTrainNum(String now, Map<String, OffTrainNumRange> offTrainNumRangeMap,int personNum){
        OffTrainNumRange offTrainNumRange = offTrainNumRangeMap.get(now);
        if (offTrainNumRange == null) {
            return personNum;
        }
        if(offTrainNumRange.isInRange(personNum)){
          return   personNum;
        }
        return offTrainNumRange.getMax();
    }



    public List<Person> getOffTrainClearPassengers(
            String stationName,
            int personNum,
            MetroTrain metroTrain) {

        List<Person> personList = getAllPersons(metroTrain);
        if(personList.size()<personNum){
            return personList;
        }else {
            return getOffTrainList(metroTrain, personNum);
        }

    }



    /**
     * @description:获取所有列车上的乘客
     *
     * @author:  renzhg
     * @date:  2024/9/23 9:15
     **/
    private List<Person> getAllPersons(MetroTrain metroTrain) {
        return metroTrain.getPersonMap().values().stream()
                .flatMap(List::stream) // 将 List<Person> 转换为 Stream<Person>
                .collect(Collectors.toList()); // 收集为 List<Person>
    }



    private List<Person> getOffTrainList(MetroTrain metroTrain, int personNum) {
        List<Person> offTrainList = new ArrayList<>();
        int count = 0;
        for (Map.Entry<String, List<Person>> entry : metroTrain.getPersonMap().entrySet()) {
            offTrainList.addAll(entry.getValue());
            count += entry.getValue().size();
            if (count >= personNum) {
                break;
            }
        }
        return offTrainList; // 返回下车的乘客列表
    }


//    public List<Person> getOffTrainClearPassengers(
//            String stationName,
//            int personNum,
//            MetroTrain metroTrain) {
//        List<Person> offTrainList = new ArrayList<>();
//        List<Person> personList = metroTrain.getPersonMap().values().stream()
//                .flatMap(List::stream) // 将 List<Person> 转换为流
//                .collect(Collectors.toList()); // 收集为 List<Person>
//        if(personList.size()<personNum){
//            personNum=personList.size();
//            return personList;
//        }else{
//            int count = 0;
//            for (Map.Entry<String, List<Person>> entry : metroTrain.getPersonMap().entrySet()) {
//                count =count+entry.getValue().size();
//                offTrainList.addAll(entry.getValue());
//                if(count>=personNum){
//                    break;
//                }
//            }
//        }
//        return offTrainList; // 返回下车的乘客列表
//    }

    private void exitStation(List<Person> pf, Station station) {
        Map<String, Person> personMap = new HashMap<>();

//        logger.debug("=====================下车的人数是：" + pf.size() + "人");
        pf.forEach(o -> {
            int outTime = station.getStationPlatform().getCrossTime() + station.getStationConcourser().getCrossTime();
//            countWalkTime
            o.setPersonState(PersonState.outStation);
            o.setOutOutputWalkTime(outTime);
            personMap.put(o.getId(), o);
        });
        station.setOutStationPersons(personMap);
//        station.getOutStationPersonMap().put(o.getId(), o);
    }
}
